package com.finance.cooperate.common.pojo;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.finance.cooperate.common.constant.CodeMsg;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName RestResult
 * @Description 自定义响应结构
 * @Author shen
 * @Date 2022/5/25 15:27
 * @Modify ...
 */
public class RestResult {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();
    // 响应业务状态
    private String status;
    // 响应消息
    private String message;
    // 响应中的数据
    private Object data;

    public static RestResult build(String status, String message, Object data) {
        return new RestResult(status, message, data);
    }

    public static RestResult build(CodeMsg codeMsg) {
        return new RestResult(codeMsg.getCode(), codeMsg.getMsg(), "");
    }

    public static RestResult ok(Object data) {
        return new RestResult(data);
    }

    public static RestResult ok() {
        return new RestResult(null);
    }

    public RestResult() {

    }

    public static RestResult build(String status, String message) {
        return new RestResult(status, message, null);
    }

    public RestResult(String status, String message, Object data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public RestResult(Object data) {
        this.status = "200";
        this.message = "SUCCESS";
        this.data = data;
    }

    public Boolean isOK() {
        return this.status == "200";
    }

    /**
     * @Author shen
     * @Description 将json结果集转化为RestResult对象
     * @Date 11:48 上午 2020/6/19
     * @Param [jsonData json数据, clazz RestResult中的object类型]
     * @return finance.CRM.common.pojo.RestResult
     **/
    public static RestResult formatToPojo(String jsonData, Class<?> clazz) {

        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, RestResult.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return build(jsonNode.get("status").asText(), jsonNode.get("message").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @Author shen
     * @Description 没有object对象的转化
     * @Date 11:48 上午 2020/6/19
     * @Param [json]
     * @return finance.CRM.common.pojo.RestResult
     **/
    public static RestResult format(String json) {
        try {
            return MAPPER.readValue(json, RestResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Author shen
     * @Description Object是集合转化
     * @Date 11:49 上午 2020/6/19
     * @Param [jsonData json数据, clazz 集合中的类型]
     * @return finance.CRM.common.pojo.RestResult
     **/
    public static RestResult formatToList(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(),
                        MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return build(jsonNode.get("status").asText(), jsonNode.get("message").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        if (Objects.isNull(this.data)) {
            this.setData(new Object());
        }
        return JSON.toJSONString(this);
    }
}
